package com.ckerchina.admin.ckadmin.service;

import java.io.File;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.ckerchina.admin.ckadmin.utils.JsonMapper;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.rs.PutPolicy;

@Service
public class QiniuService {

	@Value("${qiniu.ak}")
	private String ak;
	@Value("${qiniu.sk}")
	private String sk;
	@Value("${qiniu.bucketName}")
	private String bucketName;
	@Value("${qiniu.path}")
	public String path;

	private LoadingCache<String, String> cahceBuilder = CacheBuilder.newBuilder().maximumSize(10)
			.refreshAfterWrite(59, TimeUnit.MINUTES).expireAfterWrite(120, TimeUnit.SECONDS)
			.build(new CacheLoader<String, String>() {
				@Override
				public String load(String key) throws Exception {
					Mac mac = new Mac(getAk(), getSk());
					String bucketName = getBucketName();
					PutPolicy putPolicy = new PutPolicy(bucketName);
					String uptoken = putPolicy.token(mac);
					return uptoken;
				}
			});

	public String getUploadToken() {
		return cahceBuilder.getUnchecked("UploadToken");
	}

	/**
	 * @see http://developer.qiniu.com/docs/v6/api/reference/up/upload.html
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> uploadFile(String fileName, String file) {
		String uploadToken = getUploadToken();
		PutExtra extra = new PutExtra();
		PutRet ret = IoApi.putFile(uploadToken, fileName, file, extra);
		String response = ret.getResponse();
		Map<String, Object> map = JsonMapper.fromJson(response, Map.class);
		return map;
	}

	/**
	 * @see http://developer.qiniu.com/docs/v6/api/reference/up/upload.html
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> uploadFile(String fileName, File file) {
		String uploadToken = getUploadToken();
		PutExtra extra = new PutExtra();
		PutRet ret = IoApi.putFile(uploadToken, fileName, file, extra);
		String response = ret.getResponse();
		Map<String, Object> map = JsonMapper.fromJson(response, Map.class);
		return map;
	}

	/**
	 * @see http://developer.qiniu.com/docs/v6/api/reference/up/upload.html
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> uploadFile(String fileName, InputStream is) {
		String uploadToken = getUploadToken();
		PutExtra extra = new PutExtra();
		PutRet ret = IoApi.Put(uploadToken, fileName, is, extra);
		String response = ret.getResponse();
		Map<String, Object> map = JsonMapper.fromJson(response, Map.class);
		return map;
	}

	public String getAk() {
		return ak;
	}

	public String getSk() {
		return sk;
	}

	public String getBucketName() {
		return bucketName;
	}

	public String getPath() {
		return path;
	}

}
